package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.repository.ApUserSearchRepository;
import com.heima.search.service.IApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {

    @Autowired
    private ApUserSearchRepository userSearchRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    @Async
    public void saveRecord(ApUserSearch userSearch) {
        // 查询是否已经有对应的搜索记录
        Query query = new Query();
        query.addCriteria(Criteria.where("keyword").is(userSearch.getKeyword()));
        // 如果有用户id,根据用户id和关键字查询
        if (userSearch.getUserId() != null) {
            query.addCriteria(Criteria.where("userId").is(userSearch.getUserId()));
        } else {
            // 没有用户id,根据设备id和关键字查询
            query.addCriteria(Criteria.where("equipmentId").is(userSearch.getEquipmentId()));
        }
        boolean exists = mongoTemplate.exists(query, ApUserSearch.class);
        if (!exists) {
            // 保存搜索记录
            mongoTemplate.save(userSearch);
        }

    }

    @Override
    public ResponseResult load(ApArticleSearchDto dto) {

        // 根据当前的用户或者设备查询搜索记录
        Query query = new Query();
        User user = AppThreadLocalUtil.get();
        if (user != null) {
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        } else {
            if (StringUtils.isEmpty(dto.getEquipmentId())) {
                return ResponseResult.okResult(new ArrayList<ApUserSearch>());
            }
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        // 查询最近的5条数据
        query.with(PageRequest.of(0, dto.getSize(), Sort.Direction.DESC, "createdTime"));
        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delete(ApArticleSearchDto dto) {
        // 根据搜索记录id集合删除
        List<String> ids = new ArrayList<>();
        for (ApUserSearch userSearch : dto.getHisList()) {
            ids.add(userSearch.getId());
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(ids));
        mongoTemplate.remove(query, ApUserSearch.class);
        return ResponseResult.okResult();
    }
}
